home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / TEX-UTIL / DVIPS_55 / dvips / src / c / search < prev    next >
Text File  |  1994-05-06  |  12KB  |  443 lines

  1. /*
  2.  *   The search routine takes a directory list, separated by PATHSEP, and
  3.  *   tries to open a file.  Null directory components indicate current
  4.  *   directory. if the file SUBDIR exists and the file is a font file,
  5.  *   it checks for the file in a subdirectory named the same as the font name.
  6.  *   Returns the open file descriptor if ok, else NULL.
  7.  */
  8. #include "dvips.h" /* The copyright notice in that file is included too! */
  9. #include <ctype.h>
  10. #ifdef OS2
  11. #include <stdlib.h>
  12. FILE *fat_fopen();
  13. #endif
  14.  
  15. #if defined(SYSV) || defined(VMS) || defined(__THINK__) || defined(MSDOS) || defined(OS2)
  16. #define MAXPATHLEN (256)
  17. #else
  18. #include <sys/param.h>          /* for MAXPATHLEN */
  19. #endif
  20. #if !defined(MSDOS) && !defined(OS2)
  21. #ifndef VMS
  22. #ifndef MVSXA
  23. #ifndef VMCMS /* IBM: VM/CMS */
  24. #ifndef __THINK__
  25. #include <pwd.h>
  26. #endif
  27. #endif
  28. #endif  /* IBM: VM/CMS */
  29. #endif
  30. #endif
  31. /*
  32.  *
  33.  *   We hope MAXPATHLEN is enough -- only rudimentary checking is done!
  34.  */
  35.  
  36. #ifdef DEBUG
  37. extern integer debug_flag;
  38. #endif  /* DEBUG */
  39. extern char *mfmode ;
  40. extern int actualdpi ;
  41. char realnameoffile[MAXPATHLEN] ;
  42. FILE *
  43. search(path, file, mode)
  44.         char *path, *file, *mode ;
  45. {
  46.    extern char *getenv(), *newstring() ;
  47.    register char *nam ;                 /* index into fname */
  48.    register FILE *fd ;                  /* file desc of file */
  49.    char fname[MAXPATHLEN] ;             /* to store file name */
  50.    static char *home = 0 ;              /* home is where the heart is */
  51. #ifdef MVSXA
  52. char fname_safe[256];
  53. register int i, firstext, lastext, lastchar;
  54. #endif
  55. #ifdef VMCMS /* IBM: VM/CMS - we don't have paths or dirsep's but we strip off
  56.                              filename if there is a Unix path dirsep    */
  57.    register char *lastdirsep ;
  58.    lastdirsep = strrchr(file, '/') ;
  59.    if ( NULL != lastdirsep ) file = lastdirsep + 1 ;
  60.    if ((fd=fopen(file,mode)) != NULL) {
  61.       return(fd) ;
  62.    } else {
  63.       return(NULL) ;
  64.    }
  65. #else
  66.    if (*file == DIRSEP) {               /* if full path name */
  67.       if ((fd=fopen(file,mode)) != NULL) {
  68.          strcpy(realnameoffile, file) ;
  69.          return(fd) ;
  70.       } else
  71.          return(NULL) ;
  72.    }
  73. #endif   /* IBM: VM/CMS */
  74.  
  75. #if defined MSDOS || defined OS2
  76.    if ( isalpha(file[0]) && file[1]==':' ) {   /* if full path name */
  77.       if ((fd=fopen(file,mode)) != NULL)
  78.          return(fd) ;
  79.       else
  80.          return(NULL) ;
  81.    }
  82. #endif
  83.  
  84.    do {
  85.       /* copy the current directory into fname */
  86.       nam = fname;
  87.       /* copy till PATHSEP */
  88.       if (*path == '~') {
  89.          char *p = nam ;
  90.          path++ ;
  91.          while (*path && *path != PATHSEP && *path != DIRSEP)
  92.             *p++ = *path++ ;
  93.          *p = 0 ;
  94.          if (*nam == 0) {
  95.             if (home == 0) {
  96.                if (0 != (home = getenv("HOME")))
  97.                   home = newstring(home) ;
  98.                else
  99.                   home = "." ;
  100.             }
  101.             strcpy(fname, home) ;
  102.          } else {
  103. #if defined MSDOS || defined OS2
  104.             error("! ~username in path???") ;
  105. #else
  106. #ifdef VMS
  107.             error("! ~username in path???") ;
  108. #else
  109. #ifdef VMCMS  /* IBM: VM/CMS */
  110.             error("! ~username in path???") ;
  111. #else
  112. #ifdef MVSXA  /* IBM: MVS/XA */
  113.             error("! ~username in path???") ;
  114. #else
  115. #ifdef __THINK__
  116.             error("! ~username in path???") ;
  117. #else
  118.             struct passwd *pw = getpwnam(fname) ;
  119.             if (pw)
  120.                strcpy(fname, pw->pw_dir) ;
  121.             else
  122.                error("no such user") ;
  123. #endif
  124. #endif  /* IBM: VM/CMS */
  125. #endif
  126. #endif
  127. #endif
  128.          }
  129.          nam = fname + strlen(fname) ;
  130.       }
  131.       while (*path != PATHSEP && *path) *nam++ = *path++;
  132.       *nam = 0 ;
  133. #ifndef VMS
  134. #ifndef __THINK__
  135.       if (nam == fname) *nam++ = '.';   /* null component is current dir */
  136.  
  137.       if (*file != '\0') {
  138.          if ((nam != fname) && *(nam-1) != DIRSEP) /* GNW 1992.07.09 */
  139.             *nam++ = DIRSEP;                  /* add separator */
  140.          (void)strcpy(nam,file);                   /* tack the file on */
  141.       }
  142.       else
  143.          *nam = '\0' ;
  144. #else
  145.       (void)strcpy(nam,file);                   /* tack the file on */
  146. #endif
  147. #else
  148.       (void)strcpy(nam,file);                   /* tack the file on */
  149. #endif
  150. #ifdef MVSXA
  151. nam = fname;
  152. if (strchr(nam,'=') != NULL) {
  153.    (void) strcpy(fname_safe,fname);  /* save fname */
  154.    firstext = strchr(nam, '=') - nam + 2;
  155.    lastext = strrchr(nam, '.') - nam + 1;
  156.    lastchar  = strlen(nam) - 1;
  157.  
  158.    (void) strcpy(fname,"dd:");  /* initialize fname */
  159.    nam=&fname[3];
  160.    for (i=lastext; i<=lastchar; i++) *nam++ = fname_safe[i] ;
  161.            *nam++  = '(' ;
  162.    for (i=firstext; i<lastext-1; i++) *nam++ = fname_safe[i] ;
  163.            *nam++  = ')' ;
  164.            *nam++  = 0   ;
  165.    }
  166.    else {
  167.       if (fname[0] == '/') {
  168.          fname[0] = '\'';
  169.          strcat(&fname[strlen(fname)],"\'");
  170.       }
  171.       if (fname[0] == '.') fname[0] = ' ';
  172.       if (fname[1] == '.') fname[1] = ' ';
  173.    }
  174. #endif
  175.  
  176.       /* belated check -- bah! */
  177.       if ((nam - fname) + strlen(file) + 1 > MAXPATHLEN)
  178.          error("! overran allocated storage in search()");
  179.  
  180. #ifdef DEBUG
  181.       if (dd(D_PATHS))
  182.          (void)fprintf(stderr,"search: Trying to open %s\n", fname) ;
  183. #endif
  184.       if ((fd=fopen(fname,mode)) != NULL) {
  185.          strcpy(realnameoffile, fname) ;
  186.          return(fd);
  187.       }
  188.  
  189.    /* skip over PATHSEP and try again */
  190.    } while (*(path++));
  191.  
  192.    return(NULL);
  193.  
  194. }               /* end search */
  195.  
  196. FILE *
  197. pksearch(path, file, mode, n, dpi, vdpi)
  198.         char *path, *file, *mode ;
  199.     char *n ;
  200.     halfword dpi, vdpi ;
  201. {
  202.    extern char *getenv(), *newstring() ;
  203.    register char *nam ;                 /* index into fname */
  204.    register FILE *fd ;                  /* file desc of file */
  205.    char fname[MAXPATHLEN] ;             /* to store file name */
  206.    static char *home = 0 ;              /* home is where the heart is */
  207.    int sub ;
  208.  
  209.    if (*file == DIRSEP) {               /* if full path name */
  210.       if ((fd=fopen(file,mode)) != NULL)
  211.          return(fd) ;
  212.       else
  213.          return(NULL) ;
  214.    }
  215. #if defined MSDOS || defined OS2
  216.    if ( isalpha(file[0]) && file[1]==':' ) {  /* if full path name */
  217.       if ((fd=fopen(file,mode)) != NULL)
  218.          return(fd) ;
  219.       else
  220.          return(NULL) ;
  221.    }
  222. #endif
  223.    do {
  224.       /* copy the current directory into fname */
  225.       nam = fname;
  226.       sub = 0 ;
  227.       /* copy till PATHSEP */
  228.       if (*path == '~') {
  229.          char *p = nam ;
  230.          path++ ;
  231.          while (*path && *path != PATHSEP && *path != DIRSEP)
  232.             *p++ = *path++ ;
  233.          *p = 0 ;
  234.          if (*nam == 0) {
  235.             if (home == 0) {
  236.                if (0 != (home = getenv("HOME")))
  237.                   home = newstring(home) ;
  238.                else
  239.                   home = "." ;
  240.             }
  241.             strcpy(fname, home) ;
  242.          } else {
  243. #if defined MSDOS || defined OS2
  244.             error("! ~username in path???") ;
  245. #else
  246. #ifdef VMS
  247.             error("! ~username in path???") ;
  248. #else
  249. #ifdef VMCMS  /* IBM: VM/CMS */
  250.             error("! ~username in path???") ;
  251. #else
  252. #ifdef MVSXA  /* IBM: MVS/XA */
  253.             error("! ~username in path???") ;
  254. #else
  255. #ifdef __THINK__
  256.             error("! ~username in path???") ;
  257. #else
  258.             struct passwd *pw = getpwnam(fname) ;
  259.             if (pw)
  260.                strcpy(fname, pw->pw_dir) ;
  261.             else
  262.                error("no such user") ;
  263. #endif
  264. #endif /* IBM: VM/CMS */
  265. #endif
  266. #endif
  267. #endif
  268.          }
  269.          nam = fname + strlen(fname) ;
  270.       }
  271.       /* copy till PATHSEP */
  272.       while (*path != PATHSEP && *path) {
  273.          if (*path == '%') {
  274.             sub = 1 ;
  275.             path++ ;
  276.             switch(*path) {
  277.                case 'b': sprintf(nam, "%d", actualdpi) ; break ;
  278.                case 'd': sprintf(nam, "%d", dpi) ; break ;
  279.                case 'f': strcpy(nam, n) ; break ;
  280.                case 'm': if (mfmode == 0)
  281.                             if (actualdpi == 300) mfmode = "imagen" ;
  282.                             else if (actualdpi == 400) mfmode = "nexthi" ;
  283.                             else if (actualdpi == 635) mfmode = "linolo" ;
  284.                             else if (actualdpi == 1270) mfmode = "linohi" ;
  285.                             else if (actualdpi == 2540) mfmode = "linosuper" ;
  286.                          if (mfmode == 0)
  287.                             error("! MF mode not set, but used in pk path") ;
  288.                          strcpy(nam, mfmode) ;
  289.                          break ;
  290.                case 'p': strcpy(nam, "pk") ; break ;
  291.                case '%': strcpy(nam, "%") ; break ;
  292.                default: error("! bad format character in pk path") ;
  293.             }
  294.             nam = fname + strlen(fname) ;
  295.             if (*path)
  296.                path++ ;
  297.          } else
  298.             *nam++ = *path++;
  299.       }
  300. #ifndef VMS
  301. #ifndef __THINK__
  302.       if (nam == fname) *nam++ = '.';   /* null component is current dir */
  303. #endif
  304. #endif /* VMS */
  305.       if (sub == 0 && *file) {
  306. #ifndef VMS
  307.          /* change suggested by MG */
  308.          if ((nam != fname) && *(nam-1) != DIRSEP) /* GNW 1992.07.09 */
  309.             *nam++ = DIRSEP ;
  310. #endif
  311.          strcpy(nam, file) ;
  312.       } else
  313.          *nam = 0 ;
  314.  
  315. #ifdef MVSXA   /* IBM: MVS/XA */
  316.       if (fname[0] == '/') {
  317.          fname[0] = '\'';
  318.          strcat(&fname[strlen(fname)],"\'");
  319.       }
  320.       if (fname[0] == '.') fname[0] = ' ';
  321.       if (fname[1] == '.') fname[1] = ' ';
  322. #endif         /* IBM: MVS/XA */
  323.       /* belated check -- bah! */
  324.       if (strlen(fname) + 1 > MAXPATHLEN)
  325.          error("! overran allocated storage in search()");
  326.  
  327. #ifdef DEBUG
  328.       if (dd(D_PATHS))
  329.          (void)fprintf(stderr,"pksearch: Trying to open %s\n", fname) ;
  330. #endif
  331.       if ((fd=fopen(fname,mode)) != NULL)
  332.          return(fd);
  333.  
  334.    /* skip over PATHSEP and try again */
  335.    } while (*(path++));
  336.  
  337.    return(NULL);
  338.  
  339. }               /* end search */
  340.  
  341. /* do we report file openings? */
  342.  
  343. #ifdef DEBUG
  344. #  ifdef fopen
  345. #    undef fopen
  346. #  endif
  347. #  ifdef VMCMS  /* IBM: VM/CMS */
  348. #    define fopen cmsfopen
  349. #  endif /* IBM: VM/CMS */
  350. FILE *my_real_fopen(n, t)
  351. register char *n, *t ;
  352. {
  353.    FILE *tf ;
  354.    if (dd(D_FILES)) {
  355.       fprintf(stderr, "<%s(%s)> ", n, t) ;
  356.       tf = fopen(n, t) ;
  357.       if (tf == 0)
  358.          fprintf(stderr, "failed\n") ;
  359.       else
  360.          fprintf(stderr, "succeeded\n") ;
  361.    } else
  362.       tf = fopen(n, t) ;
  363. #ifdef OS2
  364.    if (tf == (FILE *)NULL)
  365.      tf = fat_fopen(n, t); /* try again with filename truncated to 8.3 */
  366. #endif
  367.    return tf ;
  368. }
  369. #endif
  370.  
  371. #ifdef OS2
  372. /* truncate filename at end of fname to FAT filesystem 8.3 limit */
  373. /* if truncated, return fopen() with new name */
  374. FILE *fat_fopen(fname, t)
  375. char *fname, *t;
  376. {
  377.    char *np;    /* pointer to name within path */
  378.    char nbuf[13], *ns, *nd;
  379.    char n[MAXPATHLEN];
  380.    int ni, ne;
  381.    FILE *tf;
  382.    strcpy(n, fname);
  383.    for (ns=n; *ns; ns++) {
  384.       if (*ns=='/')
  385.          *ns=DIRSEP;
  386.    }
  387.    np = strrchr(n,DIRSEP);
  388.    if (np==(char *)NULL)
  389.       np = n;
  390.    else
  391.       np++;
  392.    /* fail if it contains more than one '.' */
  393.    ni = 0;
  394.    for (ns=np; *ns; ns++) {
  395.       if (*ns=='.')
  396.          ni++;
  397.    }
  398.    if (ni>1)
  399.       return (FILE *)NULL;
  400.    /* copy it to nbuf, truncating to 8.3 */
  401.    ns = np;
  402.    nd = nbuf;
  403.    ni = 0;
  404.    while ((*ns!='.') && (*ns) && (ni<8)) {
  405.       *nd++ = *ns++;
  406.       ni++;
  407.    }
  408.    while ((*ns!='.') && (*ns)) {
  409.       ns++;
  410.       ni++;
  411.    }
  412.    ne = 0;
  413.    if (*ns=='.') {
  414.       *nd++ = *ns++;
  415.       while ((*ns!='.') && (*ns) && (ne<3)) {
  416.          *nd++ = *ns++;
  417.          ne++;
  418.       }
  419.       while (*ns) {
  420.          ns++;
  421.          ne++;
  422.       }
  423.    }
  424.    *nd++='\0';
  425.    if ((ni>8) || (ne>3)) {
  426.       strcpy(np,nbuf);
  427.       /* now code copied from my_real_fopen() */
  428.       if (dd(D_FILES)) {
  429.          fprintf(stderr, "<%s(%s)> ", n, t) ;
  430.          tf = fopen(n, t) ;
  431.          if (tf == 0)
  432.             fprintf(stderr, "failed\n") ;
  433.          else
  434.             fprintf(stderr, "succeeded\n") ;
  435.       }
  436.       else
  437.          tf = fopen(n, t) ;
  438.       return tf;
  439.    }
  440.    return (FILE *)NULL;
  441. }
  442. #endif
  443.